package com.sgzn.sgpay.web.controller;

import com.sgzn.sgpay.enums.EnumBankCode;
import com.sgzn.sgpay.model.request.*;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.MchLoginResult;
import com.sgzn.sgpay.model.response.result.MchQueryBalanceResult;
import com.sgzn.sgpay.service.SgsMchLoginService;
import com.sgzn.sgpay.service.SgsMerchantManageService;
import com.sgzn.sgpay.service.SgsMerchantQueryService;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 商户管理控制器
 * @author zhongzhongyong
 * @version Id: MerchantController.java, v 0.1 2018/5/12 14:36 zhongzhongyong Exp $$
 */
@Slf4j
@Controller
@RequestMapping("/merchant")
public class MerchantController extends MerchantBaseController {

    @Resource
    private SgsMchLoginService sgsMchLoginService;

    @Resource
    private SgsMerchantManageService sgsMerchantManageService;

    @Resource
    private SgsMerchantQueryService sgsMerchantQueryService;

    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 商户登录
     * @param request
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public SgsResultResponse<MchLoginResult> login(MchLoginRequest request) {
        return this.sgsMchLoginService.login(assembleLoginRequest(request));
    }

    /**
     * 商户入驻（注册）
     * @param request
     * @return
     */
    @RequestMapping("register")
    @ResponseBody
    public SgsResultResponse<String> register(MchRegisterRequest request) {
        return this.sgsMerchantManageService.add(assembleRegisterRequest(request));
    }

    /**
     * 商户重置密码
     * @param request
     * @return
     */
    @RequestMapping("resetPwd")
    @ResponseBody
    public SgsResultResponse<String> resetPwd(MchResetPwdRequest request) {
        return this.sgsMerchantManageService.resetPwd(assembleResetPwdRequest(request));
    }


    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping("quit")
    @ResponseBody
    public SgsResultResponse<String> quit(MchQuitRequest request) {
        super.setMchInfo(request);
        return this.sgsMchLoginService.quit(assembleQuitRequest(request));
    }

    /**
     * 商户余额查询
     * @param request
     * @return
     */
    @RequestMapping("queryBalance")
    @ResponseBody
    public SgsResultResponse<MchQueryBalanceResult> queryBalance(MchQueryRequest request) {
        super.setMchInfo(request);
        return this.sgsMerchantQueryService.queryMerchantBalanceInfo(assembleMerchantQueryRequest(request));
    }

    /**
     * 商户绑定银行卡
     * @param request
     * @return
     */
    @RequestMapping("bindBankCard")
    @ResponseBody
    public SgsResultResponse<String> bindBankCard(MchBindBankCardRequest request) {
        SgsMerchantBankCardUpdateRequest sgsMerchantBankCardUpdateRequest = new SgsMerchantBankCardUpdateRequest();
        sgsMerchantBankCardUpdateRequest.setBankAccountName( request.getAccountName() );
        sgsMerchantBankCardUpdateRequest.setBankCardNo( request.getBankCardNo() );
        sgsMerchantBankCardUpdateRequest.setBankMobileNo( request.getMobileNo() );
        sgsMerchantBankCardUpdateRequest.setBankCardCode(EnumBankCode.JSYH.getCode()); //建设银行，这里需要根据卡Bin获取卡Code
        sgsMerchantBankCardUpdateRequest.setCertCode(request.getCertCode());
        this.assembleCommonHandle.assemble(request, sgsMerchantBankCardUpdateRequest);
        return this.sgsMerchantManageService.bindMerchantBankCardInfo( sgsMerchantBankCardUpdateRequest );
    }

    /**
     * 组装商户登录请求
     * @param request
     * @return
     */
    private SgsMchLoginRequest assembleLoginRequest(MchLoginRequest request) {
        SgsMchLoginRequest target = new SgsMchLoginRequest();
        this.assembleCommonHandle.assemble(request, target);
        target.setLoginAccount(request.getLoginAccount());
        target.setPassword(request.getPassword());
        return target;
    }

    /**
     * 组装商户注册请求
     * @param request
     * @return
     */
    private SgsMerchantAddRequest assembleRegisterRequest(MchRegisterRequest request) {
        SgsMerchantAddRequest target = new SgsMerchantAddRequest();
        this.assembleCommonHandle.assemble(request, target);
        target.setMerchantName(request.getMerchantName());
        target.setPassword(request.getPassword());
        target.setConfirmPwd(request.getConfirmPwd());
        target.setContact(request.getMobile());
        target.setCode(request.getCode());
        target.setName(request.getName());
        target.setTempCode(request.getTempCode());
        return target;
    }

    /**
     * 组装商户重置密码请求
     * @param request
     * @return
     */
    private SgsMchResetPwdRequest assembleResetPwdRequest(MchResetPwdRequest request) {
        SgsMchResetPwdRequest target = new SgsMchResetPwdRequest();
        this.assembleCommonHandle.assemble(request, target);
        target.setPassword(request.getPassword());
        target.setConfirmPwd(request.getConfirmPwd());
        target.setMobile(request.getMobile());
        target.setCode(request.getCode());
        return target;
    }

    /**
     * 组装商户token校验请求
     * @param request
     * @return
     */
    private SgsMchQuitRequest assembleQuitRequest(MchQuitRequest request) {
        SgsMchQuitRequest target = new SgsMchQuitRequest();
        this.assembleCommonHandle.assemble(request,target);
        return target;
    }

    /**
     * 组装商户token校验请求
     * @param request
     * @return
     */
    private SgsMerchantQueryBalanceRequest assembleMerchantQueryRequest(MchQueryRequest request) {
        SgsMerchantQueryBalanceRequest target = new SgsMerchantQueryBalanceRequest();
        this.assembleCommonHandle.assemble(request, target);
        return target;
    }

}

